package cs2340.quarterblack.wheresmystuff;

import cs2340.quarterblack.wheresmystuff.Storage.loginResponse;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;

public class LoginPage extends Activity {
	
	Context context;
	
	Button btnSignin;
	Button btnSignup;
	EditText etAccount;
	EditText etPassword;
	TextView tvFailedLogin;
	ProgressBar waitForCommunication;
	
	Account account;
	int failedAttempts;
	boolean loginSuccessful;

	/**
	 * creates the login page
	 */
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_page);
		/*
		LocalStorage.setContext(getBaseContext());
		*/
		// Initialize UI elements
		btnSignin = (Button)findViewById(R.id.signin);
		btnSignup = (Button)findViewById(R.id.signup);
		
		etAccount = (EditText)findViewById(R.id.account);
		etPassword = (EditText)findViewById(R.id.password);
		waitForCommunication = (ProgressBar)findViewById(R.id.communicationWait);
		account = new Account("","");
		failedAttempts = 0;
		
		
		/**
		 * creates the sign in button
		 */
		btnSignin.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				// account and password entered
				String stAccount = etAccount.getText().toString();
				String stPassword = etPassword.getText().toString();
				if (!stAccount.equals(account.getUserId())) {
					account = new Account(stAccount,stPassword);
					failedAttempts = 0;
				}
				loginResponse loginSuccessful = account.checkLogin();
				if (loginSuccessful == loginResponse.ALLOWED){
					Intent mainIntent = new Intent(LoginPage.this, MainActivity.class);
					Model.setAccount(account.getAccount());
					LoginPage.this.startActivity(mainIntent);
					
				}
				else if (loginSuccessful == loginResponse.LOCKED_ACCOUNT) {
					Toast.makeText(getApplicationContext(), "Account locked - Please contact an Administrator.", 5).show();
				}
				else if (loginSuccessful != loginResponse.INVALID_CREDENTIALS){
					
					failedAttempts++;
					if (failedAttempts >= 3) {
						if (failedAttempts == 3) {
						account.lockAccount();
						}
						Toast.makeText(getApplicationContext(), "Account locked - Too many failed attempts.", 5).show();
					}
					else {
						Toast.makeText(getApplicationContext(), "Login attempt failed. " +(3 - failedAttempts)+ " attempts remaining.", 5).show();
					}
				}
				else
				{
					Toast.makeText(getApplicationContext(), "Invalid Credentials",5).show();
				}
			}
		});
		
		/**
		 * this allows the user to create an account
		 */
		btnSignup.setOnClickListener(new OnClickListener(){
			@Override
			public void onClick(View v) {
				Intent signup = new Intent(LoginPage.this,Signup.class);
				LoginPage.this.startActivity(signup);
			}
		});
	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		return true;
	}

}
